package com.fer.tankr.controllers;

import java.util.Timer;
import java.util.TimerTask;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.camera.hud.controls.AnalogOnScreenControl;
import org.andengine.engine.camera.hud.controls.AnalogOnScreenControl.IAnalogOnScreenControlListener;
import org.andengine.engine.camera.hud.controls.BaseOnScreenControl;

import android.util.Log;

import com.badlogic.gdx.math.Vector2;
import com.fer.andenginemvc.api.PhysicsController;
import com.fer.andenginemvc.api.gameObjects.GameObject;
import com.fer.tankr.GlobalState;
import com.fer.tankr.MainActivity;
import com.fer.tankr.controllers.joystick.JoyStick;
import com.fer.tankr.controllers.level.Level;
import com.fer.tankr.huds.GameHud;
import com.fer.tankr.huds.PauseMenu;
import com.fer.tankr.models.Tank;
import com.fer.tankr.models.enemies.Enemy;
import com.fer.tankr.models.powerups.Coin;
import com.fer.tankr.models.powerups.ChangeLevel.IChangeLevelHandler;
import com.fer.tankr.scenes.GameScene;

public class GameController extends PhysicsController<MainActivity> implements IChangeLevelHandler{
	
	private Level level;
	
	private JoyStick controllerMovement;
	private JoyStick controllerCannon;
	
	private GameHud hud;
	
	private PauseMenu menu;
	
	public GameController(MainActivity baseGame, Camera camera) {
		super(new GameScene(baseGame.assetManager.grassTexture,baseGame.getVertexBufferObjectManager()), baseGame, camera);
		hud = new GameHud(vbom, baseGame.assetManager.hudFont, camera.getWidth(), camera.getHeight(), baseGame.assetManager.pauseHUDButton,this, baseGame.assetManager.coinsTexture, baseGame.assetManager.bulletsTexture);
		level = new Level(this,vbom, physWorld, scene, camera, hud, baseGame.assetManager.levels, baseGame.assetManager);
		
		menu = new PauseMenu(baseGame, baseGame.assetManager.pauseMenuResume, baseGame.assetManager.pauseMenuMenu, this);
		menu.setPosition(0, 0);
		
		menu.setVisible(false);
		initGameControllers();
	}
	
	@Override
	public void onShow() {
		this.camera.setHUD(hud);
		level.initLevel(GlobalState.get().getCurrentLevel());
		camera.setChaseEntity(level.tank.entity);
		resume();
	}

	
	public void newLevel(int levelNum) {
		
		level.tearDown();
		level.initLevel(levelNum);
		toast("Prepare for level "+levelNum);
	}
	

	
	public void initGameControllers() {
	
		controllerMovement = new JoyStick(50f,(float)(camera.getHeight() - ((MainActivity)baseGame).assetManager.mOnScreenControlBaseTextureRegion.getHeight()-50) ,(MainActivity) baseGame, new IAnalogOnScreenControlListener() {
			
			@Override
			public void onControlChange(BaseOnScreenControl pBaseOnScreenControl,
					float pValueX, float pValueY) {
				if (!isPaused())
				{
					level.tank.move(pValueX, pValueY);
				}
			}
			
			@Override
			public void onControlClick(AnalogOnScreenControl pAnalogOnScreenControl) {};
		});
		scene.setChildScene(controllerMovement);

		controllerCannon = new JoyStick(camera.getWidth() - ((MainActivity)baseGame).assetManager.mOnScreenControlBaseTextureRegion.getWidth()-50,camera.getHeight() - ((MainActivity)baseGame).assetManager.mOnScreenControlBaseTextureRegion.getHeight()-50 ,(MainActivity)baseGame,new IAnalogOnScreenControlListener() {
			
			@Override
			public void onControlChange(BaseOnScreenControl pBaseOnScreenControl,
					float pValueX, float pValueY) {
				if (!isPaused())
				{
					Vector2 dir = new Vector2(pValueX,pValueY).nor();
					if(dir.len()>0.5)
					{
						GameObject<?> go = level.tank.fire(dir);
						if (go!=null && !go.entity.hasParent() )
						{
							addGameObject(go);
						}
					}
				}
			}
			
			@Override
			public void onControlClick(AnalogOnScreenControl pAnalogOnScreenControl) {}
		});
		controllerMovement.setChildScene(controllerCannon);
	}


	@Override
	public void onCollision(GameObject<?> objA, GameObject<?> objB) {
		super.onCollision(objA, objB);
		if (matches(objA, objB, Coin.class, Tank.class))
		{
			Log.d("Collision", "coin");
			if (level.reduceCoins())
			{
				level.nextLevel();
				GlobalState.get().setCurrentLevel(level.level);
				GlobalState.get().setMaxLevel(level.level);
			}
		}
		if (matches(objA, objB, Tank.class, Enemy.class))
		{
			Tank tank = get(Tank.class, objA, objB);
			baseGame.vibrate();
			if (tank.isDead())
			{
				Timer t = new Timer();
				t.schedule(new TimerTask() {
					
					@Override
					public void run() {
					}
				}, 1500);
				level.reset();
				toast("Sorry, you are dead!");
				
				
				baseGame.vibrate();
			}
		}
	}
	
	@Override
	public void onLeave() {
		level.tearDown();
		camera.getHUD().setVisible(false);
	}

	@Override
	public void pause() {

		menu.setVisible(true);
		camera.setHUD(menu);
		super.pause();
	}
	
	@Override
	public void resume() {
		menu.setVisible(false);
		camera.setHUD(hud);
		super.resume();
	}

	@Override
	public void onChangeLevel(int level) {
		this.level.tearDown();
		this.level.initLevel(level);
	}

	
}
